package com.lagou.business.listener;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Fxz
 * @version 1.0
 * @date 2021-10-05 20:07
 */
@Slf4j
@Component
@RabbitListener(queues = "ad_update_queue")
public class AdListener {

    @RabbitHandler
    public void updateAd(String message) {
        log.info("接受到的消息:{}", message);
        String url = "http://192.168.200.128/ad_update?position=" + message;
        //通过OkHttpClient发送请求
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //获取lua脚本的响应
                log.info("调用成功:{}", response.message());
            }
        });
    }
}
